//
//  UIAlertController+Extension.m
//  MrGreatRiver
//
//  Created by Mi Chuang on 2022/3/19.
//

#import "UIAlertController+Extension.h"

@implementation UIAlertController (Extension)

+ (instancetype)initwithTitle:(NSString *)title
                      message:(NSString *)message
                       titles:(NSArray *)titles
                   itemSelect:(void (^)(NSInteger))itemSelectBlock {
    return [self initwithTitle:title
                       message:message
                        titles:titles
                   cancelTitle:@"取消"
                    itemSelect:itemSelectBlock];
}

+ (instancetype)initwithTitle:(NSString *)title
                      message:(NSString *)message
                       titles:(NSArray *)titles
                 buttonColors:(NSArray *)colors
                   itemSelect:(void (^)(NSInteger))itemSelectBlock {
    return [self initwithTitle:title
                       message:message
                        titles:titles
                  cancelTitile:@"取消"
                  buttonColors:colors
                    itemSelect:itemSelectBlock];
}

+ (instancetype)initwithTitle:(NSString *)title
                      message:(NSString *)message
                       titles:(NSArray *)titles
                  cancelTitle:(NSString *)cancelTitle
                   itemSelect:(void (^)(NSInteger))itemSelectBlock {
    return [self initwithTitle:title
                       message:message
                        titles:titles
                  cancelTitile:@"取消"
                  buttonColors:nil
                    itemSelect:itemSelectBlock];
}

+ (instancetype)initwithTitle:(NSString *)title
                      message:(NSString *)message
                       titles:(NSArray *)titles
                 cancelTitile:(NSString *)cancelTitle
                 buttonColors:(NSArray *)colors
                   itemSelect:(void (^)(NSInteger))itemSelectBlock {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    for (int i = 0; i < titles.count; i ++) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:titles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            itemSelectBlock(i);
        }];
        [alertVC addAction:action];
    }
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertVC addAction:cancelAction];
    return alertVC;
}

+ (instancetype)initwithTitle:(NSString *)title
                      message:(NSString *)message
                       titles:(NSArray *)titles
                   alertStyle:(UIAlertControllerStyle)alertStyle
                   itemSelect:(void (^)(NSInteger))itemSelectBlock {
    return [self initwithTitle:title
                       message:message
                        titles:titles
                   cancelTitle:@"取消"
                    alertStyle:alertStyle
                    itemSelect:itemSelectBlock];
}

+ (instancetype)initwithTitle:(NSString *)title
                      message:(NSString *)message
                       titles:(NSArray *)titles
                 buttonColors:(NSArray *)colors
                   alertStyle:(UIAlertControllerStyle)alertStyle
                   itemSelect:(void (^)(NSInteger))itemSelectBlock {
    return [self initwithTitle:title
                       message:message
                        titles:titles
                  cancelTitile:@"取消"
                  buttonColors:colors
                    alertStyle:alertStyle
                    itemSelect:itemSelectBlock];
}

+ (instancetype)initwithTitle:(NSString *)title
                      message:(NSString *)message
                       titles:(NSArray *)titles
                  cancelTitle:(NSString *)cancelTitle
                   alertStyle:(UIAlertControllerStyle)alertStyle
                   itemSelect:(void (^)(NSInteger))itemSelectBlock {
    return [self initwithTitle:title
                       message:message
                        titles:titles
                  cancelTitile:@"取消"
                  buttonColors:nil
                    alertStyle:alertStyle
                    itemSelect:itemSelectBlock];
}

+ (instancetype)initwithTitle:(NSString *)title
                      message:(NSString *)message
                       titles:(NSArray *)titles
                 cancelTitile:(NSString *)cancelTitle
                 buttonColors:(NSArray *)colors
                   alertStyle:(UIAlertControllerStyle)alertStyle
                   itemSelect:(void (^)(NSInteger))itemSelectBlock {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:alertStyle];
    for (int i = 0; i < titles.count; i ++) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:titles[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            itemSelectBlock(i);
        }];
        [alertVC addAction:action];
    }
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertVC addAction:cancelAction];
    return alertVC;
}

@end
